package com.xuanwei.core.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.xuanwei.core.exception.XwException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/4/30 13:56
 **/
@Component
public class FastDFSClient {


    private FastFileStorageClient storageClient;

    private FdfsWebServer fdfsWebServer;

    @Autowired
    public FastDFSClient(FastFileStorageClient storageClient,FdfsWebServer fdfsWebServer){
        this.storageClient=storageClient;
        this.fdfsWebServer=fdfsWebServer;
    }



    /**
     * 上传文件
     *
     * @param file
     *            文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);

        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    /**
     * 上传图片
     * @param multipartFile 文件
     * @return 图片链接
     * @throws IOException 异常
     */
    public String uploadImage(MultipartFile multipartFile) throws IOException {
        String fileName = multipartFile.getOriginalFilename();
        if(fileName==null){
            throw new XwException("文件名为空");
        }
        String type = fileName.substring(fileName.lastIndexOf(".")+1);
         MetaData metaData=new MetaData("作者","mjy");
        Set<MetaData> set =new HashSet<>();
        set.add(metaData);
        FastImageFile fastImageFile=new FastImageFile(multipartFile.getInputStream(),multipartFile.getSize(),type,set);
        StorePath storePath = storageClient.uploadImage(fastImageFile);
        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    public String uploadFile(File file) throws IOException {
        StorePath storePath = storageClient.uploadFile(new FileInputStream(file), FileUtils.sizeOf(file),
                FilenameUtils.getExtension(file.getName()), null);

        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    public String uploadFile2(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);

        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    public String uploadQRCode(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                "png", null);

        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    public String uploadFace(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
                "png", null);

        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    public String uploadBase64(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
                "png", null);

        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    /**
     * 将一段字符串生成一个文件上传
     *
     * @param content
     *            文件内容
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
        return fdfsWebServer.getWebServerUrl()+"/"+storePath.getGroup() + "/" + storePath.getPath();
    }

    /**
     * 删除文件
     *
     * @param fileUrl
     *            文件访问地址
     * @return
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            e.getMessage();
        }
    }
}
